<?php


namespace app\common\model;


use addons\shopro\model\Order;
use addons\shopro\model\OrderItem;
use think\Db;

class ShopOrder
{

    public static function notify2($order_sn)
    {
        $order = Order::where('order_sn',$order_sn)->find();
        if($order && $order['is_qrorder'] == 1){
            if($order['status'] != 2){
                $user = User::get($order['user_id']);
                $orderItem = OrderItem::where('order_id',$order['id'])->find();
                $shopname = '';
                if($orderItem){
                    $shop = Db::table('fa_shopro_store')
                        ->where('id',$orderItem['store_id'])
                        ->find();
                    $shopname = $shop?$shop['name']:'';
                    $shops = Db::table('fa_shopro_user_store')
                        ->where('store_id',$orderItem['store_id'])
                        ->select();
                    //print_R($shops);die();
                    foreach($shops as $v){
                        $oauth = Db::table('fa_shopro_user_oauth')->where('user_id',$v['user_id'])->find();
                        //echo $oauth['openid'].PHP_EOL;
                        if(!empty($oauth['openid'])){
                            $param = [
                                'first' => '您有一笔交易',
                                'keyword1' => date('Y年m月d日 H:i'),
                                'keyword2' => $shopname,
                                'keyword3' => '用户'.$user?$user['mobile']:'',
                                'keyword4' => '消费',
                                'keyword5' =>'支付'.$order['pay_fee'].'元',
                                'remark' => $order['remark'],
                                'openid' => $oauth['openid'], //查询商户的openid
                            ];
                            $res =  ShopOrder::sendmsg($param);
                            \think\Log::log('打印发送结果start');
                            \think\Log::log($res);
                            \think\Log::log('打印发送结果end');
                            //print_R($res);
                        }
                    }
                }
            }
            $order->status = 2;
            $order->paytime = time();
            $order->save();

            $orderItem = OrderItem::where('order_id',$order['id'])->find();
            if($orderItem){
                $orderItem->dispatch_status = 2;
                $orderItem->save();
            }
        }
    }

    public static function notify($order_sn)
    {
        \think\Log::log('回调start');
        \think\Log::log($order_sn);
        \think\Log::log('回调end');
        $order = Order::where('order_sn',$order_sn)->find();

        if($order && $order['is_qrorder'] == 1){
            if($order['status'] != 2){
                $user = User::get($order['user_id']);
                $orderItem = OrderItem::where('order_id',$order['id'])->find();
                $shopname = '';
                if($orderItem){
                    $shop = Db::table('fa_shopro_store')
                        ->where('id',$orderItem['store_id'])
                        ->find();
                    $shopname = $shop?$shop['name']:'';
                    $shops = Db::table('fa_shopro_user_store')
                        ->where('store_id',$orderItem['store_id'])
                        ->select();
                    //print_R($shops);die();
                    foreach($shops as $v){
                        $oauth = Db::table('fa_shopro_user_oauth')->where('user_id',$v['user_id'])->find();
                        //echo $oauth['openid'].PHP_EOL;
                        if(!empty($oauth['openid'])){
                            $param = [
                                'first' => '您有一笔交易',
                                'keyword1' => date('Y年m月d日 H:i'),
                                'keyword2' => $shopname,
                                'keyword3' => '用户'.$user?$user['mobile']:'',
                                'keyword4' => '消费',
                                'keyword5' =>'支付'.$order['pay_fee'].'元',
                                'remark' => $order['remark'],
                                'openid' => $oauth['openid'], //查询商户的openid
                            ];
                            $res =  ShopOrder::sendmsg($param);
                            \think\Log::log('打印发送结果start');
                            \think\Log::log($res);
                            \think\Log::log('打印发送结果end');
                            //print_R($res);
                        }
                    }
                }
            }
            $order->status = 2;
            $order->paytime = time();
            $order->save();

            $orderItem = OrderItem::where('order_id',$order['id'])->find();
            if($orderItem){
                $orderItem->dispatch_status = 2;
                $orderItem->save();
            }
        }
    }

    public static function sendmsg($param)
    {
        $appId = 'wx40ca236c146a8e4f'; // 替换为您的小程序 App ID
        $secret = 'cdb3fca214b2da1f9747418365e05aea'; // 替换为您的小程序 App Secret

        $accessToken = getAccessToken($appId, $secret);

        $touser = $param['openid']; // 替换为接收统一消息的用户的openid
        $templateId = 'mz7C30a8efQwAwAbXP-ZAE67Y-p-k0QBpaA8BcYUyG0'; // 替换为统一消息的模板ID

        $page = 'pages/app/merchant/index';
        $data = array(
            'first' => array('value' => $param['first']), // 替换为模板中的参数及其对应的值
            'keyword1' => array('value' => $param['keyword1']),
            'keyword2' => array('value' => $param['keyword2']),
            'keyword3' => array('value' => $param['keyword3']),
            'keyword4' => array('value' => $param['keyword4']),
            'keyword5' => array('value' => $param['keyword5']),
            'remark' =>  array('value' => $param['remark']),
        );

       return  $response = sendUniformMessage($accessToken, $touser, $templateId, $page, $data);
    }
}
